import 'package:flutter/material.dart';
import 'package:hzy_normal_widget/hzy_normal_widget.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hzy_template/config/dataconfig/language_info_config.dart';
import '../../../config/config_index.dart';
import '../../../config/controller/user_state_controller.dart';

class HoemDrawerPage extends TTNormalStatefulWidget {
  final AnimationController iconAnimationController;
  final Function(int)? callBackIndex;

  const HoemDrawerPage({
    Key? key,
    required this.iconAnimationController,
    this.callBackIndex,
  }) : super(key: key);
  @override
  TTNormalState<TTNormalStatefulWidget> getState() {
    return _HoemDrawerPageState();
  }
}

class _HoemDrawerPageState extends TTNormalState<HoemDrawerPage> {
  /// 属性
  ///
  ///

  @override
  Color? get backgroundColor => HzyColorConfig().whitebackgroundColor;

  Widget headerimage = Container();

  int selectindex = -1;

  @override
  bool get safeAreatop => false;

  @override
  bool get safeAreabottm => false;

  /// 生命周期
  ///
  ///

  @override
  Widget createBody(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.stretch,
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Container(
          width: double.infinity,
          padding: EdgeInsets.only(top: SizeMacro.statusBarHeight),
          child: GestureDetector(
            onTap: () {
              if (widget.callBackIndex != null) {
                widget.callBackIndex!(-1);
              }
            },
            child: Container(
              padding: EdgeInsets.all(16.0.sp),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                mainAxisAlignment: MainAxisAlignment.start,
                children: <Widget>[
                  AnimatedBuilder(
                    animation: widget.iconAnimationController,
                    builder: (BuildContext context, Widget? child) {
                      return ScaleTransition(
                        scale: AlwaysStoppedAnimation<double>(
                          1.0 - (widget.iconAnimationController.value) * 0.2,
                        ),
                        child: RotationTransition(
                          turns: AlwaysStoppedAnimation<double>(
                              Tween<double>(begin: 0.0, end: 24.0)
                                      .animate(
                                        CurvedAnimation(
                                          parent:
                                              widget.iconAnimationController,
                                          curve: Curves.fastOutSlowIn,
                                        ),
                                      )
                                      .value /
                                  360),
                          child: Container(
                            width: 80,
                            height: 80,
                            decoration: BoxDecoration(
                              color: HzyColorConfig().backgroudgrey,
                              borderRadius: BorderRadius.all(
                                Radius.circular(8.r),
                              ),
                            ),
                          ),
                        ),
                      );
                    },
                  ),
                  Padding(
                    padding: const EdgeInsets.only(top: 15, left: 4),
                    child: Text(
                      UserStateController.isLogin
                          ? (UserStateController.current.user.nickname ??
                              LauchNormalMsg.hometitle)
                          : LauchNormalMsg.pleaselogin,
                      style: TextStyle(
                        fontWeight: FontWeight.w600,
                        color: HzyColorConfig().col000000,
                        fontSize: 18,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
        ),
        Gaps.vGap5,
        CommentTools.configlineSpace(),
        Expanded(
          child: ListView.builder(
            itemBuilder: createItemBuilder,
            physics: const BouncingScrollPhysics(),
            padding: const EdgeInsets.all(0.0),
            itemCount: 1,
          ),
        ),
        CommentTools.configlineSpace(),
        Column(
          children: <Widget>[
            ListTile(
              title: Text(
                LauchNormalMsg.logout,
                style: FontConfig().fontMedium16Black,
                textAlign: TextAlign.left,
              ),
              trailing: const Icon(
                Icons.power_settings_new,
                color: Colors.red,
              ),
              onTap: () {
                if (widget.callBackIndex != null) {
                  widget.callBackIndex!(-2);
                }
              },
            ),
            SizedBox(
              height: MediaQuery.of(context).padding.bottom,
            )
          ],
        ),
      ],
    );
  }

  /// 网络请求
  ///
  ///

  /// 构建视图
  ///
  ///

  Widget createItemBuilder(BuildContext context, int index) {
    return InkWell(
      splashColor: Colors.grey.withOpacity(0.1),
      highlightColor: Colors.transparent,
      onTap: () async {
        if (widget.callBackIndex != null) {
          widget.callBackIndex!(index);
        }
        configsetState(() {
          selectindex = index;
        });
      },
      child: Stack(
        children: <Widget>[
          Container(
            padding: const EdgeInsets.only(top: 8.0, bottom: 8.0),
            child: Row(
              children: <Widget>[
                const SizedBox(width: 6.0, height: 46.0),
                const Padding(
                  padding: EdgeInsets.all(4.0),
                ),
                Text(
                  LauchNormalMsg.system,
                  style: TextStyle(
                    fontWeight: FontWeight.w500,
                    fontSize: 16,
                    color: selectindex == index
                        ? HzyColorConfig().colthemes
                        : HzyColorConfig().col000000,
                  ),
                  textAlign: TextAlign.left,
                ),
              ],
            ),
          ),
          selectindex == index
              ? AnimatedBuilder(
                  animation: widget.iconAnimationController,
                  builder: (BuildContext context, Widget? child) {
                    return Transform(
                      transform: Matrix4.translationValues(
                          (MediaQuery.of(context).size.width * 0.75 - 64) *
                              (1.0 -
                                  widget.iconAnimationController.value -
                                  1.0),
                          0.0,
                          0.0),
                      child: Padding(
                        padding: const EdgeInsets.only(top: 8, bottom: 8),
                        child: Container(
                          width: MediaQuery.of(context).size.width * 0.75 - 64,
                          height: 46,
                          decoration: BoxDecoration(
                            color: Colors.blue.withOpacity(0.2),
                            borderRadius: const BorderRadius.only(
                              topLeft: Radius.circular(0),
                              topRight: Radius.circular(28),
                              bottomLeft: Radius.circular(0),
                              bottomRight: Radius.circular(28),
                            ),
                          ),
                        ),
                      ),
                    );
                  },
                )
              : const SizedBox()
        ],
      ),
    );
  }

  /// 触发方法
  ///
  ///

  @override
  void configNormalData() {}
}
